<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="onCancel"
  />
</template>

<script>
import dayjs from '@/utils/dayjs'
import { editUserProfileAPI } from '@/api'
// 1 回显数据  2 取消重置 3 确认修改
export default {
  props: {
    birthday: {
      type: String,
      required: true
    }
  },
  methods: {
    async onConfirm() {
      try {
        this.$toast.loading({
          message: '桥豆麻袋....'
        })
        const birthday = dayjs(this.currentDate).format('YYYY - MM - DD')
        await editUserProfileAPI({
          birthday
        })
        this.$emit('update:birthday', birthday)
        this.$emit('close')
        this.$toast.success('ojbk')
      } catch (error) {
        this.$toast.fail('你行不行啊 细狗')
        throw error
      }
    },
    onCancel() {
      this.$emit('close')
    }
  },
  data() {
    return {
      minDate: new Date(1920, 0, 1), //设置最小时间
      maxDate: new Date(), //最大时间设置当前时间
      currentDate: new Date(this.birthday) //当前选中时间
    }
  }
}
</script>

<style></style>
